﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBA.Web.UI;
using DBA.Common;
using DBA.Model;
using DBA.Data;
using DBA.Data.ADO;


namespace DBA.Web.StudentManage
{
    public partial class EditStudentInfo : AdminPage
    {     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.Params["id"]))
                {
                    hidID.Value = RequestHelper.QueryString("id");
                    ModifyStudentInit();                    
                }
            }
        }

        private void ModifyStudentInit()
        {
            string studentInfoID = hidID.Value;
            if (!VerificationHelper.IsNullOrEmpty<string>(studentInfoID))
            {
                Student studentInfo = dal.ModifyData.GetEntity<Student>(studentInfoID);
                if (!VerificationHelper.IsNullOrEmpty<Student>(studentInfo)) //如果学生对象不为空，那么对控件赋值
                {
                    ConvertHelper.ModelToControl<Student>(studentInfo, pan_tab.Controls);
                }

                if (studentInfo.EmployeeID != 0) 
                {
                    EmployeeInfo emp = dal.ModifyData.GetEntity<EmployeeInfo>(studentInfo.EmployeeID);
                    if (emp != null)
                    {
                        hidName.Value = emp.Name;
                    }
                }

                if (txt_Birthday.Value.Trim() != "") 
                {
                    txt_Birthday.Value = DateTime.Parse(txt_Birthday.Value).ToString("yyyy-MM-dd");
                }
                if (txt_InSchoolTime.Value.Trim() != "")
                {
                    txt_InSchoolTime.Value = DateTime.Parse(txt_InSchoolTime.Value).ToString("yyyy-MM-dd");
                }
                if (txt_GranduateTime.Value.Trim() != "")
                {
                    txt_GranduateTime.Value = DateTime.Parse(txt_GranduateTime.Value).ToString("yyyy-MM-dd");
                }
            }
        }

        DataAccessLibrary dal = CommonQuery.DataAccessLibrary;

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            Student studentInfo = ConvertHelper.ControlToModel<Student>(pan_tab.Controls);          
            if (hidID.Value.Equals(string.Empty))//添加学生信息
            {
                hdResult.Value = dal.ModifyData.Insert<Student>(studentInfo).ToString();
            }
            else //修改学生信息
            {
                studentInfo.Id = ConvertHelper.ToInt32(hidID.Value);
                if (dal.ModifyData.Update<Student>(studentInfo) == "0")
                {
                    hdResult.Value = "0";
                }
                else
                {
                    hdResult.Value = "1";
                }
            }
        }
    }
}